package com.siqi.hr.system.common.sql;

import java.util.HashMap;
import java.util.Map;

/**
 * 字段类型解析
 */
public class FieldTypeParse {
	private static FieldTypeParse fieldParse;
	private static Map<String, SFieldType> types = new HashMap<String, SFieldType>();

	private static String webREgular = "^(http|https|ftp)\\://([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&amp;%\\$\\-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.[a-zA-Z]{2,4})(\\:[0-9]+)?(/[^/][a-zA-Z0-9\\.\\,\\?\\'\\/\\+&amp;%\\$#\\=~_\\-@]*)*$";

	static {
		types.put("int", new SFieldType("int", "^[0-9]\\d*$",SFieldType.DatabaseType.INT, "整数"));
		types.put("long", new SFieldType("long", "^[0-9]\\d*$",SFieldType.DatabaseType.LONG, "长整形"));
		types.put("varchar", new SFieldType("varchar", "",SFieldType.DatabaseType.VARCHAR, "文本"));
		types.put("text", new SFieldType("text", "",SFieldType.DatabaseType.TEXT, "长文本"));
		types.put("datetime", new SFieldType("datetime", "",SFieldType.DatabaseType.DATETIME, "日期时间"));
		types.put("timestamp", new SFieldType("timestamp", "",SFieldType.DatabaseType.TIMESTAMP, "日期"));
		types.put("date", new SFieldType("date", "",SFieldType.DatabaseType.DATE, "日期"));
		types.put("bit", new SFieldType("bit", "^[0-9]\\d*$",SFieldType.DatabaseType.BIT, "bit"));
		types.put("phone", new SFieldType("phone","^\\(?\\d{3,4}[-\\)]?\\d{7,8}$",SFieldType.DatabaseType.VARCHAR, "电话"));
		types.put("decimal", new SFieldType("decimal","^[-+]?[0-9]+([.]{1}[0-9]{1,8})?$",SFieldType.DatabaseType.DECIMAL, "数值"));
		types.put("precent", new SFieldType("precent","^[0-9]+([.]{1}[0-9]{1,8})?$", SFieldType.DatabaseType.DECIMAL,"百分比"));
		types.put("currency", new SFieldType("currency","^[-+]?[0-9]+([.]{1}[0-9]{1,8})?$",SFieldType.DatabaseType.DECIMAL, "货币"));
		types.put("email",new SFieldType("email","^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})$",SFieldType.DatabaseType.VARCHAR, "邮件"));
		types.put("web", new SFieldType("web", webREgular,SFieldType.DatabaseType.VARCHAR, "网址"));
		types.put("radio", new SFieldType("radio", "",SFieldType.DatabaseType.INT, "单选框"));
		types.put("singlebox", new SFieldType("singlebox", "^[0-9]\\d*$",SFieldType.DatabaseType.INT, "下拉框"));
		types.put("mutibox", new SFieldType("mutibox", "",SFieldType.DatabaseType.VARCHAR, "下拉框（多）"));
		types.put("textarea", new SFieldType("textarea", "",SFieldType.DatabaseType.TEXT, "文本区域"));
		types.put("textblock", new SFieldType("textblock", "",SFieldType.DatabaseType.MEDIUMTEXT, "文本区域（长）"));
		types.put("autosno", new SFieldType("autosno", "",SFieldType.DatabaseType.VARCHAR, "自动编号"));
		types.put("img", new SFieldType("img", "",SFieldType.DatabaseType.VARCHAR, "图片"));
	}

	public static FieldTypeParse newInstance() {
		if (fieldParse == null) {
			fieldParse = new FieldTypeParse();
		}
		return fieldParse;
	}

	public SFieldType getSFieldType(String name) {
		return (SFieldType) types.get(name);
	}
}
